package com.jackrain.nea.common;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jackrain.nea.config.PropertiesConf;
import com.jackrain.nea.core.redis.RedisObjectSerializer;
import com.jackrain.nea.web.face.User;
import com.jackrain.nea.web.face.impl.UserImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import java.net.URI;
import java.net.URISyntaxException;


@Service
public class PostUserEvent {

    @Autowired
    private PropertiesConf pconf;

    @Autowired
    private RestTemplateUtil restTemplateUtil;

    private RedisObjectSerializer serializer = new RedisObjectSerializer();

    public User getUser(String userName) throws Exception {

        MultiValueMap<String, String> multiValueMap = new LinkedMultiValueMap<String, String>();
        JSONObject paramObject1 = new JSONObject();
        paramObject1.put("name", userName);
        multiValueMap.add("param", paramObject1.toJSONString());
        String erpUrl = pconf.getProperty("cp.port.url", "");
        URI uri = null;
        try {
            uri = new URI(erpUrl + "/fc/api/query.users");
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        String retStr = restTemplateUtil.doPostForm(uri, multiValueMap);
        JSONObject selectMenuType = JSON.parseObject(retStr);

        byte[] userB = selectMenuType.getString("user").getBytes("ISO-8859-1");
        User user = (UserImpl) serializer.deserialize(userB);
        return user;

    }

}
